var Kuo_cms = {
    /* kuo 请求是数据 , Context 返回信息, context 扩展数据 */
    $DATA:{
        code:0,
        data:null,
        msg:"",
        token:""
    },$features:{
        
    },$WANGZHAN:{

    },ExpAnSion:async function($POST){
        if($POST['subclass'] > 0){
            let $kuozan = await  this.ADMIN_expansionform( $POST['subclass']);
            if($kuozan != ""){
                let $POSTexpansion = explode(',',$kuozan);
                let $POSTexpansionform = [];
                for(let zzz_ in $POSTexpansion ){
                    let $zzz = $POSTexpansion [zzz_];
                    let $hx =  explode('-',$zzz);
                    let $key = reset($hx);
                    if(isset($POST['expansion_'+$key])){
                        if(typeof($POST['expansion_'+$key]) != "string"){
                            let $zicihuo = [];
                            for( let vvv_ in $POST['expansion_'+$key] ){
                                let $vvv = $POST['expansion_'+$key][vvv_];
                                if(typeof($vvv) != "string" ){
                                    $zicihuo.push(implode('$',  $vvv ));
                                }else{
                                    $zicihuo.push( $vvv );
                                }
                            }
                            $POSTexpansionform.push(implode(',',  $zicihuo ));
                        }else{
        
                            $POSTexpansionform.push( $POST['expansion_'+$key]);
                        }
                    }
                }
                $POST['expansion'] = implode('(_$$_)',$POSTexpansionform);
        
            }else{
                $POST['expansion'] = "";
            }
        }else{
            $POST['expansion'] = "";
        }

        return $POST['expansion'];

    },KUO_Call:async function(ac ,$fenlei ,id,adminid,token,$DATA){

        if($fenlei > 0 && id > 0){
            let  $KUOFENLEI = await  this.TYPE_find({'id':$fenlei});
            if($KUOFENLEI){
                if( $KUOFENLEI['Expand'] && $KUOFENLEI['Expand'] != ""){
                    let $chanfende = explode(',',$KUOFENLEI['Expand']);
                    if($chanfende){
                        for(let expand_ in $chanfende ){
                            let $expand = Trim($chanfende[expand_]);
                            if($expand != ""){
                                let $expand_ = explode('_',$expand);
                                if( count( $expand_ ) > 1){
                                    let $class_ = $expand_['0'];
                                    delete($expand_['0']);
                                    let $expandPOST = {};
                                    for(var $kkk in $DATA){
                                        let $vvv =$DATA[ $kkk];
                                        let $jieguo = explode($class_+'_',$kkk);
                                        if (count($jieguo) > 1) {
                                            delete($jieguo['0']);
                                            $expandPOST[implode('_',$jieguo)] = $vvv;
                                        }
                                    }
                                    $expandPOST['id'] = id;
                                    $expandPOST['Safe_AdminId'] = adminid;
                                    $expandPOST['Safe_Token']  = token;
                                    let hanshu = implode('_',$expand_)+'_'+ac;
                                    if(global["Kuo_"+$class_] && global["Kuo_"+$class_][hanshu]){
                                        await global["Kuo_"+$class_][hanshu]($expandPOST);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    },
    InStall:async function( PREFIX ){
        if(!PREFIX || PREFIX == ""){
            PREFIX = MYSQLconfig.prefix;
        }
        let $db = await db("features");
        let $fan = await $db.Where({'pluginid':"cms"}).Find();
       
        if($fan){
            return "已经安装" ;
        }

        let $sql = "INSERT INTO `"+PREFIX+"features` VALUES (NULL, 'cms', 'cms', 'layui-icon-website', 'cms系统', '常用cms 系统', '13yd', 'http://www.kuosoft.com', '1', 1, '', 1590801009, '', '', '{\"列表模版\":[\"gonglist\",\"ztlist\"],\"内容模版\":[\"gonglv\",\"paihang\",\"zhuanti\"],\"默认前缀\":[\"list-\",\"\"],\"默认风格\":[\"game\"],\"网站信息id\":[\"13\"],\"缓存时间\":[\"0\"],\"游戏id\":[\"1\"],\"排行id\":[\"4\"],\"攻略id\":[\"3\"],\"语言下标\":[\"1\"]}', 1, 'index', '{\"admin_type\":{\"name\":\"分类管理\",\"typeico\":\"layui-icon-tree\",\"link\":\"\"},\"admin_content\":{\"name\":\"内容管理\",\"typeico\":\"layui-icon-table\",\"link\":\"\"}}', 0);";
        $fan = await $db.Qurey($sql,[]);
        let $sql_ ={};
        $sql_[1] = "CREATE TABLE `"+PREFIX+"cms_content` (`id` bigint unsigned NOT NULL AUTO_INCREMENT,`name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT '' COMMENT '分类名字',`keywords` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT '' COMMENT '关键词',`describes` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT '' COMMENT '描述',`subclass` bigint unsigned DEFAULT '0' COMMENT '子分类',`url` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT '' COMMENT '内容url',`link` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT '' COMMENT '外联url',`contenttemplate` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT '' COMMENT '内容模版',`uid` bigint unsigned DEFAULT '0' COMMENT '用户uid',`adminuid` bigint unsigned DEFAULT '0' COMMENT '管理员id',`recommend` int unsigned DEFAULT '0' COMMENT '推荐参数',`sortsize` int unsigned DEFAULT '0' COMMENT '排序',`popularity` bigint unsigned DEFAULT '0' COMMENT '人气',`photo` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT '' COMMENT '图片',`photoalbum` text CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci COMMENT '图片集',`content` mediumtext CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci COMMENT '分类内容',`expansion` mediumtext CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci COMMENT '扩展数据',`atime` int unsigned DEFAULT '0' COMMENT '录入时间',`xtime` int unsigned DEFAULT '0' COMMENT '修改时间',`int1` bigint UNSIGNED NULL DEFAULT 0 COMMENT '数字1',`int2` bigint UNSIGNED NULL DEFAULT 0 COMMENT '数字2',`float1` decimal(20, 2) UNSIGNED NULL DEFAULT 0 COMMENT '金额1',`float2` decimal(20, 2) UNSIGNED NULL DEFAULT 0 COMMENT '金额2',`text1` text CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci COMMENT '第一备注',`text2` text CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci COMMENT '第二备注',`char1` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT '' COMMENT '第一文本',`char2` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT '' COMMENT '第二文本',`off` smallint unsigned DEFAULT '0' COMMENT '状态0 关闭 1 审核 2 正常',`remarks` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT '' COMMENT '备注',PRIMARY KEY (`id`),KEY `url` (`url`(250)),KEY `subclass` (`subclass`) USING BTREE,KEY `off` (`off`) USING BTREE,KEY `int1` (`int1`) USING BTREE,KEY `int2` (`int2`) USING BTREE,KEY `float1` (`float1`) USING BTREE,KEY `float2` (`float2`) USING BTREE,KEY `popularity` (`popularity`) USING BTREE,KEY `sortsize` (`sortsize`) USING BTREE,KEY `recommend` (`recommend`) USING BTREE,KEY `name` (`name`(250)) USING BTREE,KEY `keywords` (`keywords`(250)) USING BTREE,KEY `describes` (`describes`(250)) USING BTREE,KEY `sortsize_id` (`sortsize`,`id`) USING BTREE,KEY `subclass_off` (`subclass`,`off`) USING BTREE ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='cms内容';";
        $sql_[2] = "CREATE TABLE `"+PREFIX+"cms_type` (  `id` bigint unsigned NOT NULL AUTO_INCREMENT,  `name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT '' COMMENT '分类名字',  `keywords` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT '' COMMENT '关键词',  `describes` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT '' COMMENT '描述',  `subclass` bigint unsigned DEFAULT '0' COMMENT '子分类',  `url` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT '' COMMENT '内容url',  `link` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT '' COMMENT '外联url',  `listtemplate` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT '' COMMENT '列表模版',  `contenttemplate` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT '' COMMENT '内容模版',  `templatetype` smallint unsigned DEFAULT '1' COMMENT '类型 0 单页 1 列表',  `uid` bigint unsigned DEFAULT '0' COMMENT '用户uid',  `adminuid` bigint unsigned DEFAULT '0' COMMENT '管理员id',  `recommend` int unsigned DEFAULT '0' COMMENT '推荐参数',  `sortsize` int unsigned DEFAULT '0' COMMENT '排序',  `popularity` bigint unsigned DEFAULT '0' COMMENT '人气',  `photo` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT '' COMMENT '图片',  `photoalbum` text CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci COMMENT '图片集',  `content` mediumtext CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci COMMENT '分类内容',  `expansion` mediumtext CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci COMMENT '扩展数据',  `expansionform` text CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci COMMENT '扩展表单数据',  `atime` int unsigned DEFAULT '0' COMMENT '录入时间',  `xtime` int unsigned DEFAULT '0' COMMENT '修改时间',  `displayswitch` smallint unsigned DEFAULT '0' COMMENT '显示开关',  `off` smallint unsigned DEFAULT '0' COMMENT '状态0 关闭 1 审核 2 正常',  `int1` bigint UNSIGNED NULL DEFAULT 0 COMMENT '数字1',  `int2` bigint UNSIGNED NULL DEFAULT 0 COMMENT '数字2',  `float1` decimal(20, 2) UNSIGNED NULL DEFAULT 0 COMMENT '金额1',  `float2` decimal(20, 2) UNSIGNED NULL DEFAULT 0 COMMENT '金额2',  `text1` text CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci COMMENT '第一备注',`text2` text CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci COMMENT '第二备注',`char1` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT '' COMMENT '第一文本',`char2` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT '' COMMENT '第二文本', `remarks` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT '' COMMENT '备注',  `Expand` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT '' COMMENT '插件调用',  PRIMARY KEY (`id`),  KEY `url` (`url`(250)),  KEY `subclass` (`subclass`) USING BTREE,  KEY `off` (`off`) USING BTREE,  KEY `int1` (`int1`) USING BTREE,  KEY `int2` (`int2`) USING BTREE,  KEY `float1` (`float1`) USING BTREE,  KEY `float2` (`float2`) USING BTREE,  KEY `displayswitch` (`displayswitch`) USING BTREE,KEY `popularity` (`popularity`) USING BTREE,KEY `sortsize` (`sortsize`) USING BTREE,KEY `recommend` (`recommend`) USING BTREE,  KEY `name` (`name`(250)) USING BTREE,  KEY `keywords` (`keywords`(250)) USING BTREE,  KEY `describes` (`describes`(250)) USING BTREE,  KEY `subclass_off` (`subclass`,`off`) USING BTREE  ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='cms分类';";

        for(var iii in $sql_){
            await $db.Qurey($sql_[iii],[]);
        }
    
        if($fan  && $fan .affectedRows > 0){
            
            return true;
        }
        return false;
    },TYPE_BAOHAN_:async function( $id){

        if($id < 0){
            return "";
        }
       
        let $db = await db('cms_type');
        let $SUHAN = $id+',';
        let $data = await $db.Zhicha('id,subclass,off').Where({'subclass': $id,'off':2}).Select();
        if($data){
            for(var ixz in $data ){
               let $ssss = $data[ixz];
                $SUHAN += $ssss['id']+',';
                $SUHAN += await this.TYPE_BAOHAN_($ssss['id'])+',';
            }
        }
        return Trim($SUHAN,',');
   
    },ADMIN_FINDURL:async function( $urlx){
        if(!$urlx || $urlx == "" ){
            return false;
        }
        let $db = await db('cms_type');
        let $ccc = await $db.Zhicha('url').Where({'url':$urlx}).Find();
        if($ccc){
            return false;
        }
        $db  = await $db.SetTable("cms_content");
        $ccc = await $db.Zhicha('url').Where({'url':$urlx}).Find();
        if($ccc){
            return false;
        }
        return $urlx;

    },TYPE_find:async function( $where ){

        let $db = await db('cms_type');
        let $shuju = await $db.Where($where).Find();
        if($shuju){
            $shuju['href'] = KuoLink([ "cms", $shuju['url'] ]);
            $shuju['expansion'] = explode('(_$$_)',$shuju['expansion']);
            return $shuju;
        }
        return false;

    },CENTER_find:async function( $where ){

        let $db = await db('cms_content');
        let $shuju = await $db.Where($where).Find();
        if($shuju){
            $shuju['href'] = KuoLink([ "cms", $shuju['url'] ]);
            $shuju['expansion'] = explode('(_$$_)',$shuju['expansion']);
            return $shuju;
        }
        return false;

    },TYPE_select:async function( $where ,$order  ,$limit  ){

        if(!$where)$where = {};
        if(!$order)$order= '';
        if(!$limit)$limit= '';
        let $db = await db('cms_type');
   
        let $shuju = await $db.Where($where).Order($order).Limit($limit).Select();
        return $shuju ;

    },CENTER_select:async function( $where ,$order ,$limit  ){

        if(!$where)$where = {};
        if(!$order)$order= '';
        if(!$limit)$limit= '';
        let $db = await db('cms_content');
        
        let $shuju = await $db.Where($where).Limit($limit).Order($order).Select();
        return $shuju ;

    },ADMIN_expansionform:async function( $id ){
        let $db = await db('cms_type');
        let $data = await $db.Zhicha("expansionform").Where({'id':$id}).Find();
        let $datax = "";
        if(!$data){
            $datax = "";
        }else{
            $datax = $data['expansionform'];
        }
        return $datax;

    },TYPE_BAOHAN:async function( id){
        //分类包含
        let $zifuha = await this.TYPE_BAOHAN_( id);
        let $suode = {};
        let $jixi = explode(',',$zifuha);
        for( var ii in  $jixi){
            let $v = $jixi[ii];
            $suode[$v]= $v;
        }
        return implode(',',$suode);

    },ADMIN_TYPEDEL:async function($id,adminid,func_,class_,ip_){

        let $db = await db('cms_type');
        let $shuju = await $db.Where({'subclass':$id}).Select();
        if( $shuju ){
            for(let vvx_ in $shuju ){
                let $vvx = $shuju[vvx_];
                let $fan = $db.Where({'subclass':$vvx['id']}).Delete();
                if($fan){
                    Kuolog('adminlog',adminid,5, $vvx ,func_,class_,ip_);
                    await this.ADMIN_TYPEDEL($vvx['id'],adminid,func_,class_,ip_);
                }
            }
        }

    },ADMIN_FINDSUBCLASS:async function($id,$pan){

        if($id < 1){
            return true;
        }
        let $db = await db('cms_type');
        let $sss = await $db.Where( {'id' : $id }).Find();
        if($sss){
            
            if($sss['subclass'] == $pan){
                return false;
            }
            return await this.ADMIN_FINDSUBCLASS($sss['subclass'],$pan);
        }
        return true;
    },ADMIN_TYPELIST:async function($id){
        //获取列表
    
       
        let $db = await db('cms_type');
        let $fanx = await $db.Zhicha('id,subclass,name,templatetype').Where( {'subclass': $id } ).Order("sortsize desc,id asc").Select();
       
        let $zuhe = [];
        if($fanx){
            for( var vv_ in $fanx  ){
                let $vv = $fanx[vv_];
                let $shujux = {
                    'title':$vv['id']+'.'+$vv['name'],
                    'id':$vv['id'],
                    'templatetype':$vv['templatetype'],
                    'spread':true
                };
                let $cccc = await this.ADMIN_TYPELIST($vv['id']);
                if($cccc){
                    $shujux['children'] = $cccc ;
                }
                $zuhe.push($shujux) ;
            }
        }
        
        return $zuhe;


    },TongXinKey:async function($zuhe,$key,atime){
        let dangtime = Mode("Tools").Time();
        if((!atime || atime =="" || atime > dangtime || atime < dangtime - 30) && atime > 0 ){
            return true;
        }
        let $features = await Kuoplus("cms");
        let $TXMM = $features['configure']['通信密码']?$features['configure']['通信密码']['0']:"";
        if($TXMM == ""){
            return false;
        }
        if(Defaultplus != ""){
            $zuhe = LTrim($zuhe,Defaultplus+'_');
        }
        let SCMM = Md5($TXMM+$zuhe+$TXMM);
        if($key == "KuoSoft"){
            return SCMM;
        }
        if($key == "" || $key.length != 32){
            return true;
        }
        if(SCMM != $key){
            return true;
        }
        return false;
    },JieData($data,$YINCANG){
        if($YINCANG){
            for(var xxm in $YINCANG){
                let ziduan = Trim($YINCANG[xxm]);
                if(ziduan != ""){
                    if($data[ziduan]){
                        delete($data[ziduan]);
                    }
                }
            }
        }
        if($data['url']){
            $data.href = KuoLink([ "cms", $data['url'] ]);
        }
        if($data['photo'] && $data['photo'] != ""){
            $data.photo = pichttp($data.photo);
        }
        if($data['photoalbum']&& $data['photoalbum'] != ""){
            let $TUPIANJI = explode(',',$data['photoalbum']);
            $data['photoalbum'] = [];
            if($TUPIANJI){
                for(let zhi in $TUPIANJI){
                    let $photo = $TUPIANJI[zhi];
                    if($photo == ""){
                        continue ;
                    }
                    $data['photoalbum'].push(pichttp($photo));
                }
            }
        }
        if($data['content']&& $data['content'] != ""){
            let $TUPIANJI_ = Mode("Tools").TIQUimg($data['content']);
            if($TUPIANJI_){
                let $TI = [];
                let $HU = [];
                for( let $ti in $TUPIANJI_ ){
                    let $photo = $TUPIANJI_[$ti];
                    if($photo == ""){
                        continue ;
                    }
                    $TI.push($photo);
                    $HU.push(pichttp($photo));
                }
                if($TI.length > 0){
                    $data['content'] = str_replace( $TI, $HU,$data['content']);
                }
            }
        }
        return $data;
    }
}